﻿using EA.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EA.DAO;
using System.Data;
using EA.Common;

namespace EA.BA
{
   public class CustomerBA:ObjectBA
    {
        public CustomerTO GetCustomerByID(int ID)
        {
            CustomerDA customerDA = new CustomerDA(ObjectBA.ConnectionString);
            DataTable table = customerDA.GetCustomerByID(ID);
            CustomerTO customer = new CustomerTO();
            if (table.Rows.Count != 0)
            {
                customer.ID=Convert.ToInt32(table.Rows[0]["ID"]);
                customer.FullName = table.Rows[0]["FullName"].ToString();
                customer.Email=table.Rows[0]["Email"].ToString();
                customer.Password = table.Rows[0]["Password"].ToString();
                customer.Address = table.Rows[0]["Address"].ToString();
                customer.Phone =Converts.getValueString("Phone",0,table);
                customer.Sellable =Convert.ToInt32(table.Rows[0]["Sellable"]);
            }
            return customer;
        }

        public CustomerTO GetCustomerByEmail(String email)
        {
            CustomerDA customerDA = new CustomerDA(ObjectBA.ConnectionString);
            DataTable table = customerDA.GetCustomerByEmail(email);
            CustomerTO customer = new CustomerTO();
            if (table.Rows.Count != 0)
            {
                customer.ID = Convert.ToInt32(table.Rows[0]["ID"]);
                customer.FullName = table.Rows[0]["FullName"].ToString();
                customer.Email = table.Rows[0]["Email"].ToString();
                customer.Password = table.Rows[0]["Password"].ToString();
                customer.Address = table.Rows[0]["Address"].ToString();
                customer.Phone = Converts.getValueString("Phone", 0, table);
                customer.Sellable = Convert.ToInt32(table.Rows[0]["Sellable"]);
            }
            return customer;
        }

        public CustomerTO GetCustomerByProductID(int productID)
        {
            CustomerDA customerDA = new CustomerDA(ObjectBA.ConnectionString);
            DataTable table = customerDA.GetCustomerByProductID(productID);
            CustomerTO customer = new CustomerTO();
            if (table.Rows.Count != 0)
            {
                customer.ID = Convert.ToInt32(table.Rows[0]["ID"]);
                customer.FullName = table.Rows[0]["FullName"].ToString();
                customer.Email = table.Rows[0]["Email"].ToString();
                customer.Password = table.Rows[0]["Password"].ToString();
                customer.Address = table.Rows[0]["Address"].ToString();
                customer.Phone = Converts.getValueString("Phone", 0, table);
                customer.Sellable = Convert.ToInt32(table.Rows[0]["Sellable"]);
            }
            return customer;
        }

        public List<CustomerTO> GetAllCustomer(int page)
        {
            List<CustomerTO> list = new List<CustomerTO>();
            int index1 = (page - 1) * Constant.TOTAL_RECORD_VIEW + 1;
            int index2 = index1 + Constant.TOTAL_RECORD_VIEW - 1;
            CustomerDA customerDA = new CustomerDA(ObjectBA.ConnectionString);
            DataTable table1 = customerDA.GetListCustomer(index1, index2);
            for (int i = 0; i < table1.Rows.Count; i++)
            {
                CustomerTO customerTO = new CustomerTO();
                customerTO.ID = (int)table1.Rows[i]["ID"];
                customerTO.Sellable = (int)table1.Rows[i]["Sellable"];
                customerTO.Email = table1.Rows[i]["Email"].ToString();
                customerTO.FullName = table1.Rows[i]["FullName"].ToString();
                customerTO.Phone = table1.Rows[i]["Phone"].ToString();
                customerTO.Address = table1.Rows[i]["Address"].ToString();
                list.Add(customerTO);
            }
            return list;
        }

        public int GetTotalAllCustomer()
        {
            CustomerDA customerDA = new CustomerDA(ObjectBA.ConnectionString);
            DataTable table1 = customerDA.GetTotalCustomer();
            return Converts.getValueInteger("Total", 0, table1);
        }

        public List<CustomerTO> GetAllCustomer()
        {
            List<CustomerTO> list = new List<CustomerTO>();
            CustomerDA customerDA = new CustomerDA(ObjectBA.ConnectionString);
            DataTable table1 = customerDA.GetListCustomer();
            for (int i = 0; i < table1.Rows.Count; i++)
            {
                CustomerTO customerTO = new CustomerTO();
                customerTO.ID = (int)table1.Rows[i]["ID"];
                customerTO.Sellable = (int)table1.Rows[i]["Sellable"];
                customerTO.Email = table1.Rows[i]["Email"].ToString();
                customerTO.FullName = table1.Rows[i]["FullName"].ToString();
                customerTO.Phone = table1.Rows[i]["Phone"].ToString();
                customerTO.Address = table1.Rows[i]["Address"].ToString();
                list.Add(customerTO);
            }
            return list;
        }

        public Boolean InsertCustomer(CustomerTO customer)
        {
            CustomerDA customerDA = new CustomerDA(ConnectionString);
            return customerDA.InsertCustomer(customer);
        }

        public Boolean UpdateCustomer(CustomerTO customer)
        {
            CustomerDA customerDA = new CustomerDA(ConnectionString);
            return customerDA.UpdateCustomer(customer);
        }

        public Boolean DeleteCustomer(int ID)
        {
            CustomerDA customerDA = new CustomerDA(ConnectionString);
            return customerDA.DeleteCustomer(ID);
        }
    }
}
